#include <iostream>

#include "xlcomm/base/logger.h"
#include "xlcomm/net/buffer.h"
#include "xlcomm/net/detail/connector.h"
#include "xlcomm/net/detail/sockets_ops.h"
#include "xlcomm/net/event_loop.h"
#include "xlcomm/net/tcp_client.h"

using namespace xlcomm;

void OnConnect(const net::TcpConnectionPtr& conn) {
  char buff[] = "hello, I'm tcp client";
  conn->Send(buff, sizeof(buff));
}

void OnMessage(const net::TcpConnectionPtr& conn, net::Buffer* buff,
               Timestamp recv_time) {
  LOG_INFO << "read " << buff->ReadableBytes() << " bytes from "
           << conn->peer_addr().ToIpPort() << " at "
           << DateTime::Format(recv_time)
           << ", message:" << buff->ReadAllAsString();
}

int main(int argc, const char* argv[]) {
  std::cout << "-------------TcpClient\n";
  Logger::SetLogLevel(Logger::kInfo);

  net::InetAddress server_addr(5555, argc == 1 ? "127.0.0.1" : argv[1]);
  net::EventLoop loop;
  net::TcpClient client(&loop, server_addr, "my_client");
  client.set_connect_callback(OnConnect);
  client.set_message_callback(OnMessage);
  client.Connect();
  loop.Loop();
}